package {{.table.PackageName}}.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.common.model.ServiceResultModel;
import com.baomidou.mybatisplus.mapper.Wrapper;
import top.ibase4j.core.base.BaseService;
import com.lesso.util.BeanConversionUtils;
import com.lesso.util.ServiceResultUtil;
import com.lesso.util.WrapperUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import {{.table.PackageName}}.mapper.{{.table.ClassName}}Mapper;
import {{.table.PackageName}}.service.I{{.table.ClassName}}Service;
import {{.table.PackageName}}.model.{{.table.ClassName}};
import {{.table.PackageName}}.model.vo.{{.table.ClassName}}VO;
import {{.table.PackageName}}.model.dto.{{.table.ClassName}}DTO;
import top.ibase4j.core.exception.ServiceException;
import top.ibase4j.model.Pagination;

/**
 * {{.table.FunctionName}}Service业务层处理
 *
 * @author {{.table.FunctionAuthor}}
 * @since {{.table.CreateTime}}
*/
@Component
@DubboService(interfaceClass = I{{.table.ClassName}}Service.class)
public class {{.table.ClassName}}ServiceImpl extends BaseService<{{.table.ClassName}}, {{.table.ClassName}}Mapper> implements I{{.table.ClassName}}Service{


    /**
     * 分页查询
     *
     * @param pagination 查询条件
     * @return
    */
    @Override
    public Page<{{.table.ClassName}}VO> getPageList(Pagination<{{.table.ClassName}}DTO> pagination) throws ServiceException {
        {{.table.ClassName}}DTO entity = pagination.getEntity();
        Wrapper<{{.table.ClassName}}> wrapper = new EntityWrapper<>();
        Pagination<{{.table.ClassName}}> page = BeanConversionUtils.conversionObj(pagination, Pagination.class);
        WrapperUtil.getCondition(page, this.currentModelClass(), wrapper);
        Page<{{.table.ClassName}}VO> pageParam = new Page(pagination.getCurrent(), pagination.getSize());
        List<{{.table.ClassName}}> entityList = this.mapper.selectPage(pageParam, wrapper);
        pageParam.setRecords(BeanConversionUtils.conversionList(entityList, {{.table.ClassName}}VO.class));
        return pageParam;
    }

    /**
     * 查询{{.table.FunctionName}}列表
     *
     * @param {{ .table.ClassName |CaseCamelLower}} {{.table.FunctionName}}
     * @return {{.table.FunctionName}}集合
    */
    @Override
    public List<{{.table.ClassName}}VO> getList({{.table.ClassName}} {{ .table.ClassName |CaseCamelLower}}) throws ServiceException{
        Wrapper<{{.table.ClassName}}> wrapper = WrapperUtil.getCondition({{ .table.ClassName |CaseCamelLower}}, {{.table.ClassName}}.class);
        List<{{.table.ClassName}}> entityList = this.mapper.selectList(wrapper);
        return BeanConversionUtils.conversionList(entityList, {{.table.ClassName}}VO.class);
    }

    @Override
    public ServiceResultModel<{{.table.ClassName}}VO> getById({{.table.PkColumn.JavaType}} id) throws ServiceException {
        {{.table.ClassName}} model = this.mapper.selectById(id);
        {{.table.ClassName}}VO modelVO = BeanConversionUtils.conversionObj(model, {{.table.ClassName}}VO.class);
        return ServiceResultUtil.success(1L, modelVO);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResultModel add({{.table.ClassName}} model) throws ServiceException{
        // TODO addLogic
        this.save(model);
        return ServiceResultUtil.success(1L, model);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResultModel modify({{.table.ClassName}} model) throws ServiceException{
        // TODO modifyLogic
        this.mapper.updateById(model);
        return ServiceResultUtil.success("更新成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResultModel delete({{.table.PkColumn.JavaType}} id) {
        this.mapper.deleteById(id);
        return ServiceResultUtil.success("删除成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResultModel batchDelete(List<{{.table.PkColumn.JavaType}}> ids) {
        this.mapper.deleteBatchIds(ids);
        return ServiceResultUtil.success("删除成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResultModel batchDisabled(List<{{.table.PkColumn.JavaType}}> ids) {
        updateState(ids, 1);
        return ServiceResultUtil.success("禁用成功");
    }

    @Override
    public ServiceResultModel batchEnabled(List<{{.table.PkColumn.JavaType}}> ids) {
        updateState(ids, 0);
        return ServiceResultUtil.success("启用成功");
    }

   private void updateState(List<{{.table.PkColumn.JavaType}}> ids, Integer state) {
       {{.table.ClassName}} entity = new {{.table.ClassName}}();
       entity.setState(state);
       Wrapper<{{.table.ClassName}}> wrapper = new EntityWrapper<>();
       wrapper.in({{.table.ClassName}}.COL_{{.table.PkColumn.ColumnName| SnakeScreamingCase}}, ids);
       this.mapper.update(entity, wrapper);
   }

}
